package com.eastmelon.util;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

public class ImageUtil {
	// 给图片加透明度
	/**
	 * @param i
	 *            透明度
	 * @return
	 */
	public static File setAlpha(InputStream is,File savePath, int alpha) {
		BufferedImage image=null;
		BufferedImage	newImage=null;
		int width,height,rgb;
		Color color=null;
		try {
			image = ImageIO.read(is);
			width=image.getWidth();
			height=image.getHeight();
			newImage =new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
			for (int x=0; x < width; x++) {
				for (int y=0; y < height; y++) {
					rgb = image.getRGB(x, y);
					color = new Color(rgb);   
					color = new Color(color.getRed(), color.getGreen(),color.getBlue(), alpha);
					newImage.setRGB(x, y, color.getRGB());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			ImageIO.write(newImage,"png",savePath);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return savePath;
	}
}
